package com.haitao.wavebbs.tools;

import com.haitao.wavebbs.entity.tools.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.MultiValueMap;

/**
 * @author haitao
 * @data 2023/2/23
 */
public class ControllerTools {

    static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(5);
    /**
     * controller返回结果的统一封装
     * @param data 业务数据
     * @param httpStatus
     * @return
     */
    public static ResponseEntity getSuccessResponseEntity(Object data, HttpStatus httpStatus){
        return new ResponseEntity(new ResponseData("success",data), httpStatus);
    }

    /**
     * controller返回结果的统一封装
     * @param data  业务实体数据
     * @param httpStatus
     * @param stateMessage
     * @return
     */
    public static ResponseEntity getSuccessResponseEntity(Object data, HttpStatus httpStatus,String stateMessage){
        return new ResponseEntity(new ResponseData(stateMessage,data), httpStatus);
    }

    /**
     *  controller返回结果的统一封装
     * @param httpStatus
     * @param stateMessage  状态消息
     * @return
     */
    public static ResponseEntity getSuccessResponseEntity(HttpStatus httpStatus,String stateMessage){
        return new ResponseEntity(new ResponseData(stateMessage,null), httpStatus);
    }

    /**
     * controller返回结果的统一封装
     * @param data
     * @param httpStatus
     * @param headers 响应头设置
     * @return
     */
    public static ResponseEntity getSuccessResponseEntity(Object data, HttpStatus httpStatus, MultiValueMap headers){
        return new ResponseEntity(new ResponseData("success",data),headers,httpStatus);
    }

    /**
     * 对用户密码明文hash编码
     * @param password 用户密码明文
     * @return password的hash值
     */
    public static String hashEncode(String password){
        // BCrypt算法
        return  encoder.encode(password);
    }

    /**
     *
     * @param password  用户密码明文
     * @param result   之前保存的hashEncode值
     * @return  验证用户密码
     */
    public static boolean hashEncodeMatches(String password,String result){
        return encoder.matches(password, result);
    }
}
